package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.templates.L1Char_use_item;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Char_use_Item {
  public static final Log _log = LogFactory.getLog(Char_use_Item.class);
  
  private static final Map<Integer, L1Char_use_item> _offList = new HashMap<>();
  
  private static final ArrayList<Integer> _itemList = new ArrayList<>();
  
  private static final ArrayList<Integer> _itemList1 = new ArrayList<>();
  
  private static Char_use_Item _instance;
  
  public static Char_use_Item get() {
    if (_instance == null)
      _instance = new Char_use_Item(); 
    return _instance;
  }
  
  private Char_use_Item() {
    load();
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `characters_脚色使用道具`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int key = rs.getInt("id");
        int item_id = rs.getInt("item_id");
        int obj = rs.getInt("obj");
        L1Char_use_item value = new L1Char_use_item();
        value.set_item_id(item_id);
        value.set_obj(obj);
        _offList.put(Integer.valueOf(key), value);
        if (!_itemList.contains(Integer.valueOf(item_id)) && 
          !_itemList1.contains(Integer.valueOf(obj))) {
          _itemList1.add(Integer.valueOf(obj));
          _itemList.add(Integer.valueOf(item_id));
        } 
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public boolean checkAreaArmorOff(L1PcInstance pc, int itemId) {
    if (_offList == null)
      return true; 
    Iterator<Integer> iterator = _offList.keySet().iterator();
    while (iterator.hasNext()) {
      Integer key = iterator.next();
      L1Char_use_item off = _offList.get(key);
      if (off.get_item_id() == itemId && off.get_obj() == pc.getId())
        return false; 
    } 
    return true;
  }
  
  public void Add(L1PcInstance pc, int itemid) {
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      int i = 0;
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("INSERT INTO characters_脚色使用道具 SET item_id=?,obj=?");
      i++;
      pstm.setInt(i, itemid);
      i++;
      pstm.setInt(i, pc.getId());
      i++;
      pstm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
}
